{% extends 'inventory/base.html' %}
{% load static %}

{% block title %}扫码添加商品 - {{ block.super }}{% endblock %}

{% block content %}
<div class="row mb-4">
    <div class="col-12">
        <div class="card">
            <div class="card-body">
                <div class="d-flex justify-content-between align-items-center">
                    <div>
                        <h2 class="card-title mb-0">扫码添加商品</h2>
                        <p class="text-muted">通过条码快速添加商品信息</p>
                    </div>
                    <a href="{% url 'product_list' %}" class="btn btn-outline-secondary">
                        <i class="bi bi-arrow-left me-1"></i> 返回列表
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <!-- 左侧：条码扫描和结果 -->
    <div class="col-lg-5 col-md-12">
        <!-- 条码扫描和查询结果区域 -->
        <div class="card h-100 mb-4 mb-lg-0">
            <div class="card-header bg-primary text-white">
                <h5 class="card-title mb-0"><i class="bi bi-upc-scan me-2"></i>扫描条码与商品信息</h5>
            </div>
            <div class="card-body">
                <!-- 条码扫描输入框 -->
                <form method="get" action="{% url 'barcode_product_create' %}" class="mb-3">
                    <div class="input-group">
                        <span class="input-group-text"><i class="bi bi-upc"></i></span>
                        <input type="text" name="barcode" class="form-control form-control-lg" placeholder="输入或扫描条码" value="{{ barcode }}" required autofocus>
                        <button type="submit" class="btn btn-primary">
                            <i class="bi bi-search me-1"></i> 查询
                        </button>
                    </div>
                    <div class="form-text text-center mt-2">将条码扫描枪对准商品条码，或手动输入条码号码</div>
                </form>
                
                <hr class="my-3">
                
                <!-- 条码查询结果 -->
                <div class="barcode-result overflow-auto" style="max-height: 500px;">
                {% if barcode_data %}
                    <div class="text-center mb-4">
                        {% if barcode_data.image_url %}
                        <img src="{{ barcode_data.image_url }}" alt="{{ barcode_data.name }}" class="img-fluid rounded" style="max-height: 200px; max-width: 100%;">
                        {% else %}
                        <div class="bg-light rounded d-flex align-items-center justify-content-center" style="height: 200px;">
                            <i class="bi bi-image text-muted" style="font-size: 3rem;"></i>
                        </div>
                        {% endif %}
                    </div>
                    
                    <div class="mb-4 text-center">
                        <h4 class="mb-1">{{ barcode_data.name }}</h4>
                        <div class="d-flex justify-content-center align-items-center">
                            <span class="badge bg-secondary me-2">{{ barcode }}</span>
                            {% if barcode_data.category %}
                            <span class="badge bg-info text-dark">{{ barcode_data.category }}</span>
                            {% endif %}
                        </div>
                    </div>
                    
                    <div class="table-responsive">
                        <table class="table table-striped table-hover">
                            <tbody>
                                {% if barcode_data.specification %}
                                <tr>
                                    <th style="width: 35%"><i class="bi bi-rulers me-2"></i>规格</th>
                                    <td>{{ barcode_data.specification }}</td>
                                </tr>
                                {% endif %}
                                
                                {% if barcode_data.manufacturer %}
                                <tr>
                                    <th><i class="bi bi-building me-2"></i>制造商</th>
                                    <td>{{ barcode_data.manufacturer }}</td>
                                </tr>
                                {% endif %}
                                
                                {% if barcode_data.suggested_price %}
                                <tr>
                                    <th><i class="bi bi-tag me-2"></i>建议售价</th>
                                    <td class="fw-bold">¥{{ barcode_data.suggested_price }}</td>
                                </tr>
                                {% endif %}
                                
                                {% if barcode_data.description %}
                                <tr>
                                    <th><i class="bi bi-card-text me-2"></i>描述</th>
                                    <td>{{ barcode_data.description }}</td>
                                </tr>
                                {% endif %}
                            </tbody>
                        </table>
                    </div>
                    
                    <div class="d-flex justify-content-between align-items-center mt-4">
                        <div class="alert alert-success mb-0 flex-grow-1">
                            <i class="bi bi-check-circle-fill me-2"></i> 已自动填充商品信息，请确认并完善详情
                        </div>
                        <button type="button" id="quickSaveBtn" class="btn btn-success ms-3">
                            <i class="bi bi-save me-1"></i> 一键录入
                        </button>
                    </div>
                {% else %}
                    {% if barcode %}
                    <div class="alert alert-warning">
                        <i class="bi bi-exclamation-triangle-fill me-2"></i> 未找到条码 <strong>{{ barcode }}</strong> 的商品信息，请手动填写。
                    </div>
                    <div class="text-center py-5">
                        <i class="bi bi-search text-muted" style="font-size: 3rem;"></i>
                        <p class="mt-3 mb-0">没有查询到商品信息</p>
                        <small class="text-muted">您可以在右侧手动填写商品详情</small>
                    </div>
                    {% else %}
                    <div class="text-center py-5">
                        <i class="bi bi-upc-scan text-muted" style="font-size: 3rem;"></i>
                        <p class="mt-3 mb-0">请扫描或输入商品条码</p>
                        <small class="text-muted">系统将自动查询商品信息</small>
                    </div>
                    {% endif %}
                {% endif %}
                </div>
            </div>
        </div>
    </div>
    
    <!-- 右侧：商品表单 -->
    <div class="col-lg-7 col-md-12">
        <div class="card h-100 mb-4 mb-lg-0">
            <div class="card-header bg-primary text-white">
                <h5 class="card-title mb-0"><i class="bi bi-pencil-square me-2"></i>商品录入</h5>
            </div>
            <div class="card-body overflow-auto" style="max-height: 700px;">
                <form method="post" enctype="multipart/form-data" id="productForm">
                    {% csrf_token %}
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label for="id_barcode" class="form-label">商品条码</label>
                            {% if form.barcode.errors %}
                                <div class="alert alert-danger">{{ form.barcode.errors }}</div>
                            {% endif %}
                            <div class="input-group">
                                <span class="input-group-text"><i class="bi bi-upc"></i></span>
                                <input type="text" name="barcode" id="id_barcode" class="form-control" value="{{ form.barcode.value|default:barcode }}" required>
                            </div>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label for="id_name" class="form-label">商品名称</label>
                            {% if form.name.errors %}
                                <div class="alert alert-danger">{{ form.name.errors }}</div>
                            {% endif %}
                            <div class="input-group">
                                <span class="input-group-text"><i class="bi bi-tag"></i></span>
                                <input type="text" name="name" id="id_name" class="form-control" value="{{ form.name.value|default:'' }}{% if not form.name.value and barcode_data %}{{ barcode_data.name|default:'' }}{% endif %}" required>
                            </div>
                        </div>
                    </div>
                    
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label for="id_category" class="form-label">商品分类</label>
                            {% if form.category.errors %}
                                <div class="alert alert-danger">{{ form.category.errors }}</div>
                            {% endif %}
                            <div class="input-group">
                                <span class="input-group-text"><i class="bi bi-folder"></i></span>
                                <select name="category" id="id_category" class="form-select">
                                    <option value="">--选择分类--</option>
                                    {% for choice in form.category.field.choices %}
                                        {% if choice.0 %}
                                            <option value="{{ choice.0 }}" {% if form.category.value == choice.0|stringformat:"s" %}selected{% elif barcode_data and barcode_data.category == choice.1 %}selected{% endif %}>{{ choice.1 }}</option>
                                        {% endif %}
                                    {% endfor %}
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label for="id_specification" class="form-label">规格</label>
                            {% if form.specification.errors %}
                                <div class="alert alert-danger">{{ form.specification.errors }}</div>
                            {% endif %}
                            <div class="input-group">
                                <span class="input-group-text"><i class="bi bi-rulers"></i></span>
                                <input type="text" name="specification" id="id_specification" class="form-control" value="{{ form.specification.value|default:'' }}{% if not form.specification.value and barcode_data %}{{ barcode_data.specification|default:'' }}{% endif %}">
                            </div>
                        </div>
                    </div>
                    
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label for="id_price" class="form-label">售价</label>
                            {% if form.price.errors %}
                                <div class="alert alert-danger">{{ form.price.errors }}</div>
                            {% endif %}
                            <div class="input-group">
                                <span class="input-group-text">¥</span>
                                <input type="number" name="price" id="id_price" class="form-control" value="{{ form.price.value|default:'' }}{% if not form.price.value and barcode_data %}{{ barcode_data.suggested_price|default:'' }}{% endif %}" step="0.01" min="0" required>
                            </div>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label for="id_cost" class="form-label">成本价</label>
                            {% if form.cost.errors %}
                                <div class="alert alert-danger">{{ form.cost.errors }}</div>
                            {% endif %}
                            <div class="input-group">
                                <span class="input-group-text">¥</span>
                                <input type="number" name="cost" id="id_cost" class="form-control" value="{{ form.cost.value|default:'0.00' }}" step="0.01" min="0">
                            </div>
                        </div>
                    </div>
                    
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label for="id_manufacturer" class="form-label">制造商</label>
                            {% if form.manufacturer.errors %}
                                <div class="alert alert-danger">{{ form.manufacturer.errors }}</div>
                            {% endif %}
                            <div class="input-group">
                                <span class="input-group-text"><i class="bi bi-building"></i></span>
                                <input type="text" name="manufacturer" id="id_manufacturer" class="form-control" value="{{ form.manufacturer.value|default:'' }}{% if not form.manufacturer.value and barcode_data %}{{ barcode_data.manufacturer|default:'' }}{% endif %}">
                            </div>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label for="id_initial_stock" class="form-label">初始库存数量</label>
                            <div class="input-group">
                                <span class="input-group-text"><i class="bi bi-box-seam"></i></span>
                                <input type="number" name="initial_stock" id="id_initial_stock" class="form-control" value="1" min="0">
                            </div>
                        </div>
                    </div>
                    
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label for="id_color" class="form-label">颜色</label>
                            {% if form.color.errors %}
                                <div class="alert alert-danger">{{ form.color.errors }}</div>
                            {% endif %}
                            <div class="input-group">
                                <span class="input-group-text"><i class="bi bi-palette"></i></span>
                                <select name="color" id="id_color" class="form-control form-select">
                                    {% for value, text in form.fields.color.choices %}
                                        <option value="{{ value }}" {% if form.color.value == value %}selected{% endif %}>{{ text }}</option>
                                    {% endfor %}
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label for="id_size" class="form-label">尺码</label>
                            {% if form.size.errors %}
                                <div class="alert alert-danger">{{ form.size.errors }}</div>
                            {% endif %}
                            <div class="input-group">
                                <span class="input-group-text"><i class="bi bi-rulers"></i></span>
                                <select name="size" id="id_size" class="form-control form-select">
                                    {% for value, text in form.fields.size.choices %}
                                        <option value="{{ value }}" {% if form.size.value == value %}selected{% endif %}>{{ text }}</option>
                                    {% endfor %}
                                </select>
                            </div>
                        </div>
                    </div>
                    
                    <div class="mb-3">
                        <div class="form-check">
                            <input type="checkbox" name="is_active" id="id_is_active" class="form-check-input" {% if form.is_active.value %}checked{% else %}checked{% endif %}>
                            <label for="id_is_active" class="form-check-label">启用商品</label>
                            {% if form.is_active.errors %}
                                <div class="alert alert-danger mt-1">{{ form.is_active.errors }}</div>
                            {% endif %}
                        </div>
                    </div>
                    
                    <div class="mb-3">
                        <label for="id_description" class="form-label">商品描述</label>
                        {% if form.description.errors %}
                            <div class="alert alert-danger">{{ form.description.errors }}</div>
                        {% endif %}
                        <textarea name="description" id="id_description" class="form-control" rows="3">{{ form.description.value|default:'' }}{% if not form.description.value and barcode_data %}{{ barcode_data.description|default:'' }}{% endif %}</textarea>
                    </div>
                    
                    <div class="mb-4">
                        <label for="id_image" class="form-label">商品图片</label>
                        {% if form.image.errors %}
                            <div class="alert alert-danger">{{ form.image.errors }}</div>
                        {% endif %}
                        <input type="file" name="image" id="id_image" class="form-control" accept="image/*">
                        <div class="form-text">支持JPG、PNG格式，建议尺寸500x500像素</div>
                        <div class="image-preview mt-2"></div>
                    </div>
                    
                    <div class="d-grid gap-2 d-md-flex justify-content-md-end">
                        <a href="{% url 'product_list' %}" class="btn btn-secondary me-md-2">
                            <i class="bi bi-x-circle me-1"></i> 取消
                        </a>
                        <button type="submit" class="btn btn-primary">
                            <i class="bi bi-check-circle me-1"></i> 保存商品
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

{% block extra_js %}
<script>
    document.addEventListener('DOMContentLoaded', function() {
        // 自动计算成本价为售价的80%
        const priceInput = document.getElementById('id_price');
        const costInput = document.getElementById('id_cost');
        
        if(priceInput && costInput) {
            priceInput.addEventListener('input', function() {
                if (this.value && (!costInput.value || costInput.value == '0.00')) {
                    costInput.value = (parseFloat(this.value) * 0.8).toFixed(2);
                }
            });
        }
        
        // 一键录入按钮点击事件
        const quickSaveBtn = document.getElementById('quickSaveBtn');
        if(quickSaveBtn) {
            quickSaveBtn.addEventListener('click', function() {
                document.getElementById('productForm').submit();
            });
        }
        
        // 图片预览功能
        const imageInput = document.getElementById('id_image');
        if(imageInput) {
            imageInput.addEventListener('change', function() {
                if (this.files && this.files[0]) {
                    const reader = new FileReader();
                    reader.onload = function(e) {
                        const imgPreview = document.querySelector('.image-preview');
                        if(imgPreview) {
                            imgPreview.innerHTML = `<img src="${e.target.result}" class="img-fluid rounded" style="max-height: 200px;">`;
                        }
                    }
                    reader.readAsDataURL(this.files[0]);
                }
            });
        }
    });
</script>
{% endblock %}
{% endblock %}